﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Statistics;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.Statistics
{
    public partial class FrmHouseStatPara : Office2007Form 
    {
        private StatLayer lyr = null;
        private List<StatCode> codes;
        private string clause = null;

        public StatLayer Layer
        {
            set { lyr = value; }
        }

        //public List<StatCode> Codes
        //{
        //    //get { return codes; }
        //    set { codes = value; }
        //}
        public string WhereClause
        {
            get { return clause; }
        }

        public FrmHouseStatPara()
        {
            InitializeComponent();
        }

        private void FrmHouseStatPara_Load(object sender, EventArgs e)
        {
            AddDataToTreeview();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            GetStatcodeByTreeview();
            if(clause ==null||clause.Trim()=="")
            {
                MsgHelper.ShowInfomationMsgbox("为选择需要统计的房屋类型，请先选择！");
                return;
            }           
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            //codes.Clear();
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void AddDataToTreeview()
        {
            if (lyr.StatCodes  == null ||lyr.StatCodes.Count == 0)
            {
                return;
            }
            foreach (StatCode code in lyr.StatCodes)
            {
                TreeNode pNode=tvwHouse.Nodes.Add(code.Value);
                pNode.Checked = true;
                pNode.Tag = code;
            }
        }

        private void GetStatcodeByTreeview()
        {
            StringBuilder strBuilder = new StringBuilder();
            foreach (TreeNode pNode in tvwHouse.Nodes)
            {
                if (pNode.Checked == true)
                {
                    StatCode code = pNode.Tag as StatCode;
                    strBuilder.Append(" or " +lyr.StatFieldName + "='" + code.Fcode  + "'"); 
                }
            }
            clause = strBuilder.ToString();
            if (clause.StartsWith(" or "))
                clause=clause.Substring(4);
        }

        private void tvwHouse_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ShowTreeContextmenu(sender as TreeView);
            }
        }

        private void ShowTreeContextmenu(TreeView pTree)
        {
            ContextMenuStrip tvwMenuStrip = new ContextMenuStrip();
            ToolStripMenuItem pSelAllTool = new ToolStripMenuItem("全选");
            pSelAllTool.Click += delegate(object sender1, EventArgs e1)
            {
                foreach (TreeNode pNode in pTree.Nodes)
                {
                    if (pNode.Checked == false)
                        pNode.Checked = true;
                }
            };
            ToolStripMenuItem pSelNoTool = new ToolStripMenuItem("全部不选");
            pSelNoTool.Click += delegate(object sender1, EventArgs e1)
            {
                foreach (TreeNode pNode in pTree.Nodes)
                {
                    if (pNode.Checked)
                        pNode.Checked = false;
                }
            };
            tvwMenuStrip.Items.AddRange(new ToolStripItem[]
                                                {
                                                    pSelAllTool,
                                                    pSelNoTool 
                                                });
            pTree.ContextMenuStrip = tvwMenuStrip;
        }

    }
}
